package com.qcu.controller.user;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcu.annotation.LimitRate;
import com.qcu.annotation.RequireAuthorization;
import com.qcu.entity.Comment;
import com.qcu.exception.RepeatException;
import com.qcu.service.CommentService;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import com.qcu.util.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Name: MyComputer
 * @ClassName UserCommentController
 * @Description:
 * @date 2025/2/15 16:56
 */
@RestController
@RequestMapping("/user/comment")
public class UserCommentController {
    @Resource
    CommentService commentService;

    /**
     * 根据帖子id获取评论
     * @param postId
     * @return
     */
    @GetMapping("/{postId}")
    @LimitRate(key = "getUserComment",permitsPerSecond = 2,expire = 10,msg = "八嘎雅鹿，你滴点那么快滴干什么滴干活...")
    @RequireAuthorization({"USER"})
    public Response get(@PathVariable String postId,@RequestParam(required = false) Integer page,@RequestParam(required = false) Integer size){
        Page<Comment> pageParam = new Page<>(page,size);
        Page<Comment> list = commentService.getComByPID(pageParam,postId);
        return Response.success(list);
    }
    /**
     * 保存评论
     * @param comment
     * @return
     */
    @PostMapping
    @RequireAuthorization({"USER"})
    public Response save(@RequestBody Comment comment)throws RepeatException {
        try {
            Map<String,Object> map = ThreadLocalUtil.get();
            Integer userId = (Integer) map.get("id");
            comment.setUserId(userId);
            commentService.save(comment);
            return Response.success();
        } catch (Exception e) {
            throw new RepeatException(e.getMessage());

        }
    }
    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @RequireAuthorization({"USER"})
    public Response delete(@PathVariable String id){
        commentService.removeById(id);
        return Response.success();
    }
}
